package com.sky.controller.admin;


import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/admin/dish")
@Api("菜品相关接口")
public class DishController {


    @Autowired
    DishService dishService;

    @Autowired
    RedisTemplate redisTemplate;


    /**
     * 添加菜品
     *
     * @param dishDTO
     * @return
     */
    @PostMapping
    @ApiOperation("添加菜品")
    public Result save(@RequestBody DishDTO dishDTO) {
        // 添加的菜品是停售状态的,在前端不会显示,所以不需要清缓存
        dishService.saveDishAndDishFlavor(dishDTO);
        return Result.success();
    }

    /**
     * 分页查询
     *
     * @param dishPageQueryDTO
     * @return
     */
    @ApiOperation("菜品分页查询")
    @GetMapping("/page")
    public Result<PageResult> page(DishPageQueryDTO dishPageQueryDTO) {
        PageResult pageResult = dishService.page(dishPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 删除菜品
     *
     * @param ids
     * @return
     */
    @DeleteMapping
    @ApiOperation("删除菜品")
    public Result delByIds(@RequestParam List<Long> ids) {
        // 清缓存 只能删除停售的菜品,停售的菜品不在小程序中显示,如果改状态,在改状态的时候就已经清空缓存了
        // todo 注意redis的key特征  最好使用我们自己封装的RedisTemplate
        // String DISH_ALL = "DISH_*";
        // Set keys = redisTemplate.keys(DISH_ALL);
        // redisTemplate.delete(keys);

        dishService.delByIds(ids);
        return Result.success();
    }


    /**
     * 根据id查询菜品
     *
     * @param id
     * @return
     */
    @GetMapping("{id}")
    @ApiOperation("根据id查询菜品")
    public Result<DishVO> findById(@PathVariable Long id) {
        DishVO dishVO = dishService.findById(id);
        return Result.success(dishVO);
    }

    /**
     * 修改菜品
     *
     * @param dishDTO
     * @return
     */
    @PutMapping
    @ApiOperation(value = "修改菜品")
    public Result update(@RequestBody DishDTO dishDTO) {
        // 清缓存
        Long categoryId = dishDTO.getCategoryId();
        String DISH_ID = "DISH_" + categoryId;
        redisTemplate.delete(DISH_ID);

        dishService.update(dishDTO);
        return Result.success();
    }


    /**
     * 修改状态
     *
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation("更改状态")
    public Result updateStatus(@PathVariable Integer status, Long id) {
        dishService.updateStatus(status, id);
        return Result.success();
    }

    /**
     * 根据分类查询菜品
     *
     * @param categoryId
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("根据分类Id查询菜品")
    public Result<List<Dish>> findByCategoryId(Long categoryId) {
        List<Dish> list = dishService.findByCategoryId(categoryId);
        return Result.success(list);
    }
}
